<?php

namespace app\admin\model\cms;

use think\Model;
use traits\model\SoftDelete;

/**
 * 广场-文章评论关联
 * Class PlazaCommentModel
 * @package app\admin\model\cms
 */
class PlazaCommentModel extends Model
{


    // 表名
    protected $name = 'plaza_comment';

    //软删除
    use SoftDelete;

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    //自动写入时间戳的格式
    protected $dateFormat = 'Y-m-d H:i:s';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'category_name',//分类名称
        'post_name'//文章名称
    ];

    //获取文章名称 $append=>post_name属性赋值
    public function getPostNameAttr($value, $data)
    {
        $value = $value ? $value : $data['plaza_post_id'];
        $name  = PlazaPostModel::where('id', '=', $value)->value('postname');
        return !empty($name) ? $name:'';
    }

    //获取分类名称
    public function getCategoryNameAttr($value, $data)
    {
        $value = $value ? $value : $data['plaza_post_id'];
        $plaza_category_id = PlazaPostModel::where('id',$value)->value('plaza_category_id');
        $name  = PlazaCategorymodel::where('id', '=', $plaza_category_id)->value('platename');
        return !empty($name) ? $name:'';
    }




    public function post()
    {
        return $this->belongsTo('app\admin\model\cms\PlazaPostModel', 'plaza_post_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    //多对一关联用户
    public function user(){
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
